//
//  ICMBalanceAddCouponView.m
//  ICrEAM
//
//  Created by ysw on 2016/11/26.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMBalanceAddCouponView.h"
#import "ICMBottomBarView.h"
#import "ICMTitleTextFeildView.h"
#import "ICMBalanceCouponCell.h"
#import "ICMBalanceCouponModel.h"
#import "NSDate+convenience.h"
#import "ICMSelecteItemDataView.h"

#define displayWidth ScreenWidth-ActualWidth(30)*2

@interface ICMBalanceAddCouponView () <ICMBottomBarViewDelegate,UITableViewDataSource,UITableViewDelegate,ICMBalanceCouponCellDelegate,ICMSelecteItemDataViewDelegate,UIGestureRecognizerDelegate>



@property (nonatomic, strong) ICMTitleTextFeildView *couponPriceView;       // 优惠券金额
@property (nonatomic, strong) UITableView *couponTableView;
@property (nonatomic, copy) ICMSelecteItemDataView *discountItemsModeView;  //优惠券类型下拉框
@property (nonatomic, strong) ICMTitleTextFeildView *discountModeView;      //优惠券类型
    
@property (nonatomic, strong) UIView *mainDisplayView;

@end

@implementation ICMBalanceAddCouponView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = BackgroundColor_Tanslucent_black;
        UITapGestureRecognizer *backgroundGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeView)];
        backgroundGesture.delegate = self;
        [self addGestureRecognizer:backgroundGesture];
        
        UIView *mainDisplayView = [[UIView alloc] initWithFrame:CGRectMake(ActualWidth(30), ActualHeight(100), displayWidth, ActualHeight(550))];
        mainDisplayView.backgroundColor = [UIColor whiteColor];
        BorderColor(mainDisplayView, BorderColor_Gray_Hex);
        BorderWidth(mainDisplayView, 1.0);
        [self addSubview:mainDisplayView];
        self.mainDisplayView = mainDisplayView;
        
        //头部
        UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, displayWidth, ActualHeight(70))];
        [mainDisplayView addSubview:titleView];
        
        NSDictionary *titleDic = @{@"font":BoldNaviFont,@"color":TitleColor};
        UILabel *titleLabel = [self createLabelWithDic:titleDic text:@"优惠券" frame:CGRectMake(0, ActualHeight(25), displayWidth, ActualHeight(25))];
        [titleView addSubview:titleLabel];
        [titleLabel sizeToFit];
        titleLabel.center = CGPointMake(ScreenWidth/2, ActualHeight(70)/2);
        
        UIView *horSep1 = [[UIView alloc]initWithFrame:CGRectMake(0, ActualHeight(70)-1, displayWidth, ActualHeight(1))];
        horSep1.backgroundColor = SepColor_gray;
        [titleView addSubview:horSep1];
        
        //中间部分
        UIView *addView = [[UIView alloc]initWithFrame:CGRectMake(0, titleView.bottomEdge, displayWidth, ActualHeight(130))];
        [mainDisplayView addSubview:addView];
        
        
        
        
        ICMTitleTextFeildView *discountModeView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(ActualWidth(40), titleView.bottomEdge + ActualHeight(30), ActualWidth(220), ActualHeight(63)) title:@"优惠券类型" addTarget:self selector:@selector(clickDiscountModeView)];
       
        [self.mainDisplayView addSubview:discountModeView];
        self.discountModeView = discountModeView;

        
        
        ICMTitleTextFeildView *couponPriceView = [ICMTitleTextFeildView createEditableICMTitleTextFeildViewWithFrame:CGRectMake(discountModeView.rightEdge+ActualWidth(80), ActualHeight(30), ActualHeight(220), ActualHeight(63)) title:@"优惠券金额"];
        self.couponPriceView = couponPriceView;
        [addView addSubview:self.couponPriceView];
        
        UIButton *addButton = [[UIButton alloc]initWithFrame:CGRectMake(couponPriceView.rightEdge+ActualWidth(40), ActualHeight(56), ActualWidth(60), ActualHeight(34))];
        [addButton setTitle:@"增加" forState:UIControlStateNormal];
        [addButton setBackgroundColor:TitleColor];
        [addButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [addButton addTarget:self action:@selector(addCoupon) forControlEvents:UIControlEventTouchUpInside];
        addButton.layer.cornerRadius = 4;
        addButton.layer.masksToBounds = YES;
        [addView addSubview:addButton];
        
        UIView *horSep2 = [[UIView alloc]initWithFrame:CGRectMake(0, ActualHeight(130)-1, displayWidth, ActualHeight(1))];
        horSep2.backgroundColor = SepColor_gray;
        [addView addSubview:horSep2];
        
        //表格部分
        UIView *tableView = [[UIView alloc] initWithFrame:CGRectMake(0, addView.bottomEdge, displayWidth, ActualHeight(60)*6)];
        [mainDisplayView addSubview:tableView];
        [tableView addSubview:[self setupTableHeaderView]];
        self.couponTableView = [[UITableView alloc]initWithFrame:CGRectMake(ActualWidth(20), ActualHeight(50), displayWidth-ActualWidth(20)*2, ActualHeight(30)*5)];
        self.couponTableView.delegate = self;
        self.couponTableView.dataSource = self;
        self.couponTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.couponTableView.bounces = NO;
        [self.couponTableView registerClass:[ICMBalanceCouponCell class] forCellReuseIdentifier:@"CouponCell"];
        [tableView addSubview:self.couponTableView];
        
        //按钮
        ICMBottomBarView *bottomBarView = [ICMBottomBarView createBottomBarViewWithTitles:@[@"保存"] normalImages:@[[UIImage imageNamed:@"btn_save"]] seletedImages:nil andFrame:CGRectMake(0, ActualHeight(550)-70, displayWidth, 70)];
        bottomBarView.delegate = self;
        [mainDisplayView addSubview:bottomBarView];

    }
    return self;
}

/*ICMSelecteItemDataViewDelegate*/
- (void)didSelectedItemTitle:(NSString *)itemTitle{
    self.discountModeView.contentText = itemTitle;
//    [_discountItemsModeView removeFromSuperview];
}

- (UIView *)setupTableHeaderView{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(ActualWidth(20), ActualHeight(25), displayWidth-ActualWidth(20)*2, ActualHeight(30))];
    headView.backgroundColor = TitleColor;
    
    NSDictionary *headDic = @{@"font":NaviFont,@"color":ContentTextColor_Black};
    UILabel *operateLabel = [self createLabelWithDic:headDic text:@"操作" frame:CGRectMake(0, ActualHeight(7), ActualWidth(50), ActualHeight(15))];
    [headView addSubview:operateLabel];
    
    UIView *firstSep = [[UIView alloc] initWithFrame:CGRectMake(operateLabel.rightEdge, 0, 1, headView.height)];
    firstSep.backgroundColor = BorderColor_Gray;
    [headView addSubview:firstSep];
    
    UILabel *orderLabel = [self createLabelWithDic:headDic text:@"序号" frame:CGRectMake(firstSep.rightEdge, operateLabel.y, ActualWidth(50), ActualHeight(15))];
    [headView addSubview:orderLabel];
    
    UIView *secondSep = [[UIView alloc] initWithFrame:CGRectMake(orderLabel.rightEdge, 0, 1, headView.height)];
    secondSep.backgroundColor = BorderColor_Gray;
    [headView addSubview:secondSep];
    
    UILabel *userNameLabel = [self createLabelWithDic:headDic text:@"客户名称" frame:CGRectMake(secondSep.rightEdge, operateLabel.y, ActualWidth(130), ActualHeight(15))];
    [headView addSubview:userNameLabel];
    
    UIView *thirdSep = [[UIView alloc] initWithFrame:CGRectMake(userNameLabel.rightEdge, 0, 1, headView.height)];
    thirdSep.backgroundColor = BorderColor_Gray;
    [headView addSubview:thirdSep];
    
    UILabel *couponTypeLabel = [self createLabelWithDic:headDic text:@"优惠券类型" frame:CGRectMake(thirdSep.rightEdge, operateLabel.y, ActualWidth(120), ActualHeight(15))];
    [headView addSubview:couponTypeLabel];
    
    UIView *fourthSep = [[UIView alloc] initWithFrame:CGRectMake(couponTypeLabel.rightEdge, 0, 1, headView.height)];
    fourthSep.backgroundColor = BorderColor_Gray;
    [headView addSubview:fourthSep];
    
    UILabel *couponPriceLabel = [self createLabelWithDic:headDic text:@"优惠券金额" frame:CGRectMake(fourthSep.rightEdge, operateLabel.y, ActualWidth(120), ActualHeight(15))];
    [headView addSubview:couponPriceLabel];
    
    UIView *fifthSep = [[UIView alloc] initWithFrame:CGRectMake(couponPriceLabel.rightEdge, 0, 1, headView.height)];
    fifthSep.backgroundColor = BorderColor_Gray;
    [headView addSubview:fifthSep];
    
    UILabel *quantityLabel = [self createLabelWithDic:headDic text:@"优惠券数量" frame:CGRectMake(fifthSep.rightEdge, operateLabel.y, ActualWidth(120), ActualHeight(15))];
    [headView addSubview:quantityLabel];
    
    UIView *sixthSep = [[UIView alloc] initWithFrame:CGRectMake(quantityLabel.rightEdge, 0, 1, headView.height)];
    sixthSep.backgroundColor = BorderColor_Gray;
    [headView addSubview:sixthSep];
    
    UILabel *usedQuantityLabel = [self createLabelWithDic:headDic text:@"已使用数量" frame:CGRectMake(sixthSep.rightEdge, operateLabel.y, ActualWidth(120), ActualHeight(15))];
    [headView addSubview:usedQuantityLabel];
    
    UIView *seventhSep = [[UIView alloc] initWithFrame:CGRectMake(usedQuantityLabel.rightEdge, 0, 1, headView.height)];
    seventhSep.backgroundColor = BorderColor_Gray;
    [headView addSubview:seventhSep];
    
    UILabel *timeLabel = [self createLabelWithDic:headDic text:@"赠送时间" frame:CGRectMake(seventhSep.rightEdge, operateLabel.y, headView.width-usedQuantityLabel.rightEdge, ActualHeight(15))];
    [headView addSubview:timeLabel];
    
    return headView;
}


- (UILabel *)createLabelWithDic:(NSDictionary *)dic text:(NSString *)text frame:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.font = dic[@"font"];
    label.textColor = dic[@"color"];
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}

//移除视图
- (void)removeView {
    [self removeFromSuperview];
}

//点击增加按钮
- (void)addCoupon {
    if (StrIsZero_gdm(self.discountModeView.contentText)) {
        [self showString:@"请选择优惠券类型！" forSeconds:1.5];
        return;
    }else if(StrIsZero_gdm(self.couponPriceView.contentText)){
        [self showString:@"请输入优惠券金额！" forSeconds:1.5];
        return;
    }
    else if(![self isPureFloat:self.couponPriceView.contentText]){
        [self showString:@"请输入正确的优惠券金额！" forSeconds:1.5];
        return;
    }
    ICMBalanceCouponModel *model = [[ICMBalanceCouponModel alloc] init];
    model.CUSTOMER_CODE = self.OWNER_NO;
    model.CUSTOMER_NAME = self.OWNER_NAME;
    NSString *couponType = [ICrEAMTool getDeatilCodeWithParameterDesc:self.discountModeView.contentText withCode:ICMEnumIntToString(ICMEnumCouponTypePre)];
    model.COUPON_TYPE = couponType?couponType:@"0";
    model.COUPON_TYPE_NAME = self.discountModeView.contentText;
    model.COUPON_UVAL = [NSString stringWithFormat:@"%.2f",[self.couponPriceView.contentText floatValue]];
    model.COUPON_QUANTITY = @"1";
    model.USED_QUANTITY = @"0";
    model.COUPON_OUT_DATE = [[NSDate date] separatorStyleDateTimeStringToMinAndSec];
    [self.dataArray addObject:model];
    [self.couponTableView reloadData];
}

#pragma mark -- tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ICMBalanceCouponCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CouponCell"];
    if (cell == nil) {
        cell = [[ICMBalanceCouponCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CouponCell"];
    }
    cell.delegate = self;
    cell.indexPath = indexPath;
    [cell creatCellWithModel:self.dataArray[indexPath.row]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return ActualHeight(30);
}
    
#pragma mark -- 优惠券选择
- (void)clickDiscountModeView{
    NSArray *discountModeDataArray = [ICrEAMTool getParameterArrayWithCode:ICMEnumIntToString(ICMEnumCouponTypePre)];
    NSMutableArray *clickDataArray = [[ICrEAMTool getParameterTitleArrayWithArray:discountModeDataArray] mutableCopy];
    _discountItemsModeView = [ICMSelecteItemDataView createSelecteItemDataViewWithSourceView:self.discountModeView andItemDatas:clickDataArray frame:self.mainDisplayView.frame];
    _discountItemsModeView.delegate = self;
    [self.mainDisplayView addSubview:_discountItemsModeView];
}
    
    

#pragma mark -- ICMBalanceCouponCellDelegate
//删除操作
-(void)deleteCouponWithCell:(ICMBalanceCouponCell *)cell {
    [self.dataArray removeObjectAtIndex:cell.indexPath.row];
    [self.couponTableView reloadData];
}

#pragma mark -- ICMBottomBarViewDelegate
- (void)bottomBarViewDidSelectIndex:(NSInteger)index {
    [self startToSaveCouponToServer];
}

- (NSMutableDictionary *)setupSaveData
{
    NSMutableDictionary *dataDic = [NSMutableDictionary dictionary];
    NSMutableArray *coupons = [[NSMutableArray alloc] init];
    for(int i = 0;i<self.dataArray.count;i++)
    {
        ICMBalanceCouponModel *model = self.dataArray[i];
        NSDictionary *couponModelDic = [model yy_modelToJSONObject];
        [coupons addObject:couponModelDic];
    }
    [dataDic setObject:coupons forKey:@"TT_CUSTOMER_COUPON"];
    return dataDic;
}

- (void)startToSaveCouponToServer{
    if ([self.delegate respondsToSelector:@selector(startToSaveCouponToServer)]) {
        [self.delegate startToSaveCouponToServer];
    }
}

- (NSMutableArray *)dataArray
{
    if(!_dataArray)
    {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
    
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
        if ([touch.view isDescendantOfView:self.mainDisplayView]) {
            return NO;
        }
        return YES;
}

#pragma mark 工具
#pragma mark 浮点数判断
- (BOOL)isPureFloat:(NSString *)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    float val;
    return [scan scanFloat:&val] && [scan isAtEnd];
}

@end
